//
//  ALDBManage+Cache.m
//  ios-loan-assistant
//
//  Created by hcs on 2018/9/11.
//  Copyright © 2018年 guoqingping. All rights reserved.
//

#import "ALDBManage+Cache.h"

@implementation ALDBManage (Cache)
- (BOOL)addCacheVal:(id)val forKey:(NSString *)key{
    ALDBManage *dbManage = [ALDBManage standarCacheDBManage];
    NSData *theIndexData=[NSKeyedArchiver archivedDataWithRootObject:val];
    NSString *base64Encoded = [theIndexData base64EncodedStringWithOptions:0];
    NSString *sql=[NSString stringWithFormat:@"INSERT INTO %@ (key,val) VALUES('%@','%@')",kCacheTableName,key,base64Encoded];
    BOOL isSuc= [dbManage insertWithSQLStr:sql];
    return isSuc;
}
- (id)cacheValForKey:(NSString *)key{
    ALDBManage *dbManage = [ALDBManage standarCacheDBManage];
    
    NSString *sql=[NSString stringWithFormat:@"select * from %@ where key = '%@';",kCacheTableName,key];
    id val;
    FMResultSet *fs=[dbManage selectWithSQLStr:sql];
    if ([fs next]) {
        NSData *nsdataFromBase64String = [[NSData alloc]
                                          initWithBase64EncodedString:[fs stringForColumn:@"val"] options:0];
        val=[NSKeyedUnarchiver unarchiveObjectWithData:nsdataFromBase64String];
    }
    return val;
}
@end
